package com.sky.service.user.impl;

import cn.hutool.core.date.DateException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.exception.NoCourierException;
import com.sky.mapper.user.OrdersMapper;
import com.sky.pojo.Courier;
import com.sky.pojo.Orders;
import com.sky.result.PageResult;
import com.sky.service.admin.OrderDispatchService;
import com.sky.service.user.OrderService;
import com.sky.service.user.WxPayService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private OrderDispatchService orderDispatchService;
    @Override
    public PageResult OrderQuery(OrdersPageQueryDTO queryDTO) {
        if (null==queryDTO){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        if (queryDTO.getPage()<=0){
            queryDTO.setPage(1);
        }
        if (queryDTO.getPageSize()<=0){
            queryDTO.setPageSize(10);
        }
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
        Page<OrderVO> page =ordersMapper.selectPage(queryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public OrderStatisticsVO statistics() {
        return ordersMapper.countStatus();
    }

    @Override
    public OrderVO selectById(Long orderId) {
        return ordersMapper.selectQuanById(orderId);
    }

    @Override
    public void rejection(OrdersRejectionDTO dto) throws IOException {
        if (null==dto||null==dto.getId()){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        Orders orders = ordersMapper.selectById(dto.getId());
        if (orders.getStatus().equals(Orders.TO_BE_CONFIRMED)){
            orders.setStatus(Orders.CANCELLED_ORDER);
            orders.setRejectionReason(dto.getRejectionReason());
            ordersMapper.updateRejection(orders);
            wxPayService.refunds(orders,dto.getRejectionReason());
        }
    }


    //商家接单
    @Override
    public Courier confirm(OrdersConfirmDTO dto) throws InterruptedException {
        if (null == dto || null == dto.getId()){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        OrderVO orderVO = ordersMapper.selectById(dto.getId());
        if (null==orderVO){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
            //3.订单调度，系统派单，查询骑手，生成运单  -- 后续待实现
            Courier courier = orderDispatchService.dispatch(dto.getId(),null,null);
        if (null==courier){
            for (int i = 0; i < 6; i++) {
                TimeUnit.SECONDS.sleep(10);
                 courier = orderDispatchService.dispatch(dto.getId(),null,null);
                if (null!=courier)break;
            }
        }
        if (null==courier){
            throw new NoCourierException("暂时找不到骑手，请稍后再试");
        }
        orderVO.setStatus(Orders.CONFIRMED);
        ordersMapper.update(orderVO);
        return courier;
    }
}
